#include <gtest/gtest.h>
#include "../stdlib/zdefs.h"

TEST(DEFSTEST, BITFUNC) { 
  
  ASSERT_FALSE(is_all_ones(0));
  ASSERT_TRUE(is_all_zeros(0));
  ASSERT_EQ(4, min(4, 5));
  ASSERT_EQ(5, max(4, 5));
  ASSERT_EQ(1 , mask(1));
  ASSERT_EQ(7 , mask(3));
  ASSERT_TRUE(is_power_of_2(16));
  ASSERT_FALSE(is_power_of_2(15));

#if __LP64__

  ASSERT_EQ(8, sizeof(usword_t));
  ASSERT_EQ(63, count_leading_zeros(1));
  ASSERT_EQ(62, count_leading_zeros(2));
  ASSERT_EQ(0, count_leading_zeros(((usword_t)1 << 63)));

  ASSERT_EQ(64u, bits_per_word);
  ASSERT_EQ(1, rotate_bits_left((usword_t)1 << 63, 1));
  ASSERT_EQ(4, rotate_bits_left(1, 2));
  ASSERT_EQ((usword_t)1 << 63, rotate_bits_right(1, 1));

  ASSERT_EQ(64, count_trailing_bits((usword_t)1 << 63));

#else
  ASSERT_EQ(31, count_leading_zeros(1));
  ASSERT_EQ(30, count_leading_zeros(2));
  ASSERT_EQ(0, count_leading_zeros(1 << 31));
  ASSERT_EQ(2147483648u, rotate_bits_right(1, 1));
  ASSERT_EQ(32, count_trailing_bits(1 << 31));

#endif

  ASSERT_EQ(1, rotate_bits_right(2, 1));
  ASSERT_EQ(4, ilog2(16));
  ASSERT_EQ(0, ilog2(1));
  ASSERT_EQ(2, count_trailing_bits(3));
  EXPECT_EQ(7, trailing_zeros(1 << 3));
  EXPECT_EQ(3, trailing_ones(3));
  EXPECT_EQ(0, trailing_ones(2));
  EXPECT_EQ(3, count_trailing_zeros(8));
  EXPECT_EQ(4, count_trailing_ones(15));
}
